/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectReggresionDialog.java
 *
 * Created on 2011-09-10, 15:07:20
 */
package JFreeChartExtensions.ChartPanelDialogs;

import JFreeChartExtensions.chartextensions.utils.AverageCalculator;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.Series.XYRegressionSeries;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import org.jfree.data.xy.XYIntervalSeries;

/**
 *
 * @author Nobody
 */
public class SelectAverageDialog extends ChartPanelDialog {

    private AverageCalculator averageCalculator;

    /** Creates new form SetScaleDialog */
    public SelectAverageDialog(ExtendableChartPanel chartPanel, AverageCalculator averageCalculator) {
        super(chartPanel);
        initComponents();
        this.averageCalculator = averageCalculator;
        fillComboBoxesWithValues();
    }

    private void fillComboBoxesWithValues() {
        fillRegressionComboBoxWithValues();
    }

    private void fillRegressionComboBoxWithValues() {
        List<XYIntervalSeries> series = averageCalculator.getAllSeriesThatHasPointsInSelection();
        if (series.size() == 1) {
            seriesComboBox.getModel().setSelectedItem(series.get(0).getKey());
            seriesComboBox.setEnabled(false);
        } else {
            seriesComboBox.setEnabled(true);
            DefaultComboBoxModel model = (DefaultComboBoxModel) seriesComboBox.getModel();
            for (int i = 0; i < series.size(); ++i) {
                if (!(series.get(i) instanceof XYRegressionSeries)) {
                    model.addElement(series.get(i).getKey());
                }
            }
            seriesComboBox.getModel().setSelectedItem(series.get(0).getKey());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        OkCancelPanel = new javax.swing.JPanel();
        OkButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        SeriesPanel = new javax.swing.JPanel();
        seriesComboBox = new javax.swing.JComboBox();
        RegressionPanel = new javax.swing.JPanel();
        intervalInput = new javax.swing.JTextField();
        intervalLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        OkButton.setText("Ok");
        OkButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                OkButtonMouseClicked(evt);
            }
        });
        OkCancelPanel.add(OkButton);

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });
        OkCancelPanel.add(CancelButton);

        SeriesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Series"));

        javax.swing.GroupLayout SeriesPanelLayout = new javax.swing.GroupLayout(SeriesPanel);
        SeriesPanel.setLayout(SeriesPanelLayout);
        SeriesPanelLayout.setHorizontalGroup(
            SeriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SeriesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(seriesComboBox, 0, 217, Short.MAX_VALUE)
                .addContainerGap())
        );
        SeriesPanelLayout.setVerticalGroup(
            SeriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SeriesPanelLayout.createSequentialGroup()
                .addComponent(seriesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        RegressionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Properties"));

        intervalLabel.setText("interval : ");

        javax.swing.GroupLayout RegressionPanelLayout = new javax.swing.GroupLayout(RegressionPanel);
        RegressionPanel.setLayout(RegressionPanelLayout);
        RegressionPanelLayout.setHorizontalGroup(
            RegressionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RegressionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(intervalLabel)
                .addGap(18, 18, 18)
                .addComponent(intervalInput, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addContainerGap())
        );
        RegressionPanelLayout.setVerticalGroup(
            RegressionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RegressionPanelLayout.createSequentialGroup()
                .addGroup(RegressionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(intervalInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(intervalLabel))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(SeriesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(RegressionPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(OkCancelPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(RegressionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(SeriesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(OkCancelPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    private int getSelectedInterval(JTextField intervalInput) {
        String interval = intervalInput.getText();
        int intervalInt;

        try {
            intervalInt = Integer.parseInt(interval);
            if (intervalInt <= 1) {
                throw new IllegalStateException("Interval must be larger than 1");
            }
        } catch (NumberFormatException e) {
            throw new IllegalStateException("Cannot extract number " + e.getMessage());

        }
        return intervalInt;
    }

    private void OkButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OkButtonMouseClicked
       // try {
            int seriesIdx = (seriesComboBox.isEnabled()) ? seriesComboBox.getSelectedIndex() : 0;
            int intervalInt = getSelectedInterval(intervalInput);
            averageCalculator.setSeriesIdx(seriesIdx);
            averageCalculator.setInterval(intervalInt);
          /*  ProgressDialog progressDialog = averageCalculator.getProgressDialog();
            progressDialog = new ProgressDialog();
            ProgressDialog.PerformLongAction(progressDialog, averageCalculator);*/
            dispose();
       /* } catch (InterruptedException ex) {
            Main.getInstance().showErrorPopupForModalWindow(ex.getMessage(), this);
        } catch (IllegalStateException ex) {
            Main.getInstance().showErrorPopupForModalWindow(ex.getMessage(), this);
        }*/

    }//GEN-LAST:event_OkButtonMouseClicked

 /*   @Override
    public void dispose() {
        frameWithChart.setEnabled(true);
        super.dispose();
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton OkButton;
    private javax.swing.JPanel OkCancelPanel;
    private javax.swing.JPanel RegressionPanel;
    private javax.swing.JPanel SeriesPanel;
    private javax.swing.JTextField intervalInput;
    private javax.swing.JLabel intervalLabel;
    private javax.swing.JComboBox seriesComboBox;
    // End of variables declaration//GEN-END:variables
}
